#include "admin.h"
#include "ui_admin.h"
#include<QFont>
#include<QVariantList>
#include<QSqlRecord>
#include<QAbstractItemView>
#include<QVariant>
#include<QSqlQuery>
Admin::Admin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Admin)
{
    ui->setupUi(this);
    //管理员界面设计
    setFixedSize(900,800);
    setWindowTitle("管理系统");
    //设置控件字体大小
    QFont ft;
    ft.setPointSize(12);
    ui->label->setFont(ft);
    ui->label_2->setFont(ft);
    ui->label_3->setFont(ft);
    ui->label_4->setFont(ft);
    ui->comboBox->setFont(ft);
    ui->comboBox->setFixedSize(QSize(210, 28));
    ui->groupBox->setFont(ft);
    ui->pushButton->setFont(ft);
    ui->pushButton_2->setFont(ft);
    ui->pushButton_3->setFont(ft);
    ui->pushButton_4->setFont(ft);
    ui->pushButton_5->setFont(ft);
    ui->pushButton_6->setFont(ft);

    //设置模型，即导入表
    mode=new QSqlTableModel(this);
    //设置手动提交动作
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);




}

Admin::~Admin()
{
    delete ui;
}

//查询用户信息按钮
void Admin::on_pushButton_3_clicked()
{
    mode->setTable("user_info");
    //设置手动提交动作
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);
    //将模型放在tableView中
    ui->tableView->setModel(mode);
    //显示模型里的数据
    mode->select();
    mode->setHeaderData(0,Qt::Horizontal,"用户账号");
    mode->setHeaderData(1,Qt::Horizontal,"用户名");
    mode->setHeaderData(2,Qt::Horizontal,"用户密码");
    mode->setHeaderData(3,Qt::Horizontal,"用户身份");
    ///筛选条件///
//    mode->setFilter("user_id='1'");
    //设置查询模式下不允许修改
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

}
    //添加用户信息
void Admin::on_pushButton_clicked()
{
    mode->setTable("user_info");
    //设置手动提交动作
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);
    QString userId=ui->lineEdit_userId->text();
    QString userName=ui->lineEdit_name->text();
    QString userPwd=ui->lineEdit_pwd->text();
    QString identity=ui->comboBox->currentText();
//    qDebug()<<"添加的是："<<identity;
    if(userId==""||userName==""||userPwd=="")
    {
        QMessageBox::warning(this,"警告","请输入完整的信息");
    }
    else
    {
        QSqlQuery query;
        query.prepare("select * from user_info where user_id=?;");
        query.addBindValue(userId);
        query.exec();
        query.next();
        if(query.value(1).toString()=="")
        {
            //对密码进行md5加密

            QString md5Pwd=QCryptographicHash::hash(userPwd.toLatin1(),QCryptographicHash::Md5).toHex();
            //开始添加
            query.exec("select * from user_info;");
            query.last();
            query.exec(QString("insert into user_info values(%1,'%2','%3','%4');").arg(userId)
                       .arg(userName).arg(md5Pwd).arg(identity));
//            query.prepare("insert into user_info(user_id,name,pwd,identity) values(?,?,?,?);");
//            QVariantList id;
//            id<<userId;
//            QVariantList name;
//            name<<userName;
//            QVariantList pwd;
//            pwd<<md5Pwd;
//            QVariantList iden;
//            iden<<identity;
//            query.addBindValue(id);
//            query.addBindValue(name);
//            query.addBindValue(pwd);
//            query.addBindValue(iden);
//            query.execBatch();
            ui->tableView->setModel(mode);
            mode->select();
            mode->setHeaderData(0,Qt::Horizontal,"用户账号");
            mode->setHeaderData(1,Qt::Horizontal,"用户名");
            mode->setHeaderData(2,Qt::Horizontal,"用户密码");
            mode->setHeaderData(3,Qt::Horizontal,"用户身份");
            ui->lineEdit_name->setText("");
            ui->lineEdit_userId->setText("");
            ui->lineEdit_pwd->setText("");

        }
        else
        {
            QMessageBox::warning(this,"警告","用户ID已存在");
        }
    }


}
    //删除用户信息
void Admin::on_pushButton_2_clicked()
{
    //设置手动提交动作
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);
//    //1.删除一行
      //获取选中行
//    int curRow=ui->tableView->currentIndex().row();
    //获取选中行的某一数值
//    QAbstractItemModel *model=ui->tableView->model();
//    QModelIndex index = model->index(curRow,0);
//    QVariant data = model->data(index);
//    int data1=data.toInt();
//    qDebug()<<data1;
//    //执行删除选中行动作
//    mode->removeRow(curRow);

    //2. 可同时删除多行
    //获取选中的模型
    QItemSelectionModel *sMode=ui->tableView->selectionModel();
    //取出模型中的索引
    QModelIndexList list=sMode->selectedRows();
    for(int i=0;i<list.size();i++)
    {
        mode->removeRow(list.at(i).row());
    }


    int flag=QMessageBox::warning(this,"删除","你确定删除选中行吗？",QMessageBox::Yes,QMessageBox::No);
    if(flag==QMessageBox::No)
    {
        //撤销删除动作
        mode->revertAll();
        mode->submitAll();
    }
    else
    {
        mode->submitAll();
    }
}
   //修改用户信息
void Admin::on_pushButton_4_clicked()
{
    mode->setTable("user_info");
    //设置手动提交动作
    ui->tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    mode->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(mode);
    mode->select();
    mode->setHeaderData(0,Qt::Horizontal,"用户账号");
    mode->setHeaderData(1,Qt::Horizontal,"用户名");
    mode->setHeaderData(2,Qt::Horizontal,"用户密码");
    mode->setHeaderData(3,Qt::Horizontal,"用户身份");
    mode->setEditStrategy(QSqlTableModel::OnFieldChange);
}
    //查看机房信息
void Admin::on_pushButton_5_clicked()
{
    mode->setTable("room_info");
    ui->tableView->setModel(mode);
    mode->select();
    mode->setHeaderData(0,Qt::Horizontal,"机房号");
    mode->setHeaderData(1,Qt::Horizontal,"机房名字");
    mode->setHeaderData(2,Qt::Horizontal,"机房容量");
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
   //清空预约
void Admin::on_pushButton_6_clicked()
{
   int flag=QMessageBox::warning(this,"Affirm","确定要清空预约记录吗？",QMessageBox::Yes,QMessageBox::No);
   if(flag==QMessageBox::Yes)
   {
       QSqlQuery query;
       query.exec("delete from order_info;");
   }
}
